<?php
namespace app\index\controller;
use app\index\controller\Base;
use app\index\model\Set;
use app\index\model\Game;
use app\index\model\Pool;
use app\index\model\Box;
use app\index\model\Media;
use app\index\model\Instance;
use think\Db;
use think\Request;

class Qn extends Base
{
    /**
     * 写入页面装饰信息
     *
     * @return void
     * @author 
     **/
    public function set()
    {
        $data=Request::instance()->param();
        $shop_id=$data['shop_id'];
        $check=$this->check('set');
        if($check){
            $set=new Set;
            $set->allowField(true)->save($data,['shop_id'=>$shop_id]);
            $this->success();
        }else{
            $set=new Set($data);
            $set->allowField(true)->save();
            $this->success(); 
        }
    }
     /**
     * 获取页面装饰信息
     *
     * @return void
     * @author 
     **/
    public function getSet()
    {
        $shop_id=Request::instance()->param('shop_id');
        $data=Db::name('set')->where(['shop_id'=>$shop_id])->find();
        $this->success($data);
    }
    /**
     * 写入玩法逻辑信息
     *
     * @return void
     * @author 
     **/
    public function game()
    {
        $data=Request::instance()->param();
        $shop_id=$data['shop_id'];
        $check=$this->check('game');
        if($check){
            $game=new Game;
            $game->allowField(true)->save($data,['shop_id'=>$shop_id]);
            $this->success();
        }else{
            $game=new Game($data);
            $game->allowField(true)->save();
            $this->success(); 
        }
    }
        /**
     * 获取玩法逻辑信息
     *
     * @return void
     * @author 
     **/
    public function getGame()
    {
        $shop_id=Request::instance()->param('shop_id');
        $data=Game::get(['shop_id'=>$shop_id]);
        $this->success($data);
    }
    /**
     * 获取奖池信息
     *
     * @return void
     * @author 
     **/
    function getPool()
    {
        $shop_id=Request::instance()->param('shop_id');
        $data=Pool::get(['shop_id'=>$shop_id]);
        $this->success($data);
    }
    /**
     * 写入奖池信息
     *
     * @return void
     * @author 
     **/
    public function pool()
    {
        $data=Request::instance()->param();
        $shop_id=$data['shop_id'];
        $check=$this->check('pool');
        if($check){
            $pool=new Pool;
            $pool->allowField(true)->save($data,['shop_id'=>$shop_id]);
            $this->success();
        }else{
            $pool=new Pool($data);
            $pool->allowField(true)->save();
            $this->success(); 
        }

    }
    /**
     * 获取中奖信息
     *
     * @return void
     * @author 
     **/
    public function box()
    {
        $shop_id=Request::instance()->param('shop_id');
        $data=Box::all(['shop_id'=>$shop_id,'status'=>1]);
        $this->success($data);
        

    }
    /**
     * 获取最新实例化信息
     *
     * @return void
     * @author 
     **/
    public function getInstance()
    {
        $shop_id=Request::instance()->param('shop_id');
        $instance=Instance::all(['shop_id'=>$shop_id]);
         $instanceNum=count($instance);
        if($instanceNum==0){
            $data=null;
        }else{
             $data=$instance[$instanceNum-1];
        }
        $this->success($data);
        

    }
    /**
     * 写入最新实例化信息
     *
     * @return void
     * @author 
     **/
    public function writeInstance()
    {
        $data=Request::instance()->param();
        $instance=new Instance($data);
        $instance->allowField(true)->save();
        $this->success($data);
        

    }
    /**
     * 检查数据库中是否含有shop_id
     *
     * @return void
     * @author 
     **/
    public function check($db)
    {
        //$data=Request::instance()->param();
        $shop_id=Request::instance()->param('shop_id');
        $data=Db::name($db)->where(['shop_id'=>$shop_id])->find();
        if(count($data)==0){
            return false;
        }else{return true;}

    }
   /**
     * 获取媒体库
     *
     * @return void
     * @author 
     **/
    function getMedia()
    {
        $data=Media::all();
        $this->success($data);
    }


}
